package com.cropflow.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;

import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {
    
    // 个人中心页面
    @GetMapping("/profile")
    public String profilePage(Model model) {
        // 模拟用户数据
        Map<String, Object> user = new HashMap<>();
        user.put("name", "张三");
        user.put("phone", "138****8888");
        user.put("farmName", "绿野农场");
        user.put("farmArea", "500亩");
        user.put("joinDate", "2023-01-15");
        
        model.addAttribute("user", user);
        model.addAttribute("stats", getUserStats());
        return "user/profile";
    }
    
    // 登录API
    @PostMapping("/api/login")
    @ResponseBody
    public ResponseEntity<?> login(@RequestBody Map<String, String> loginData) {
        String phone = loginData.get("phone");
        String password = loginData.get("password");
        
        // 简单验证逻辑
        if ("13800000000".equals(phone) && "123456".equals(password)) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "登录成功");
            result.put("redirectUrl", "/dashboard");
            return ResponseEntity.ok(result);
        }
        
        Map<String, Object> result = new HashMap<>();
        result.put("success", false);
        result.put("message", "手机号或密码错误");
        return ResponseEntity.badRequest().body(result);
    }
    
    // 注册API
    @PostMapping("/api/register")
    @ResponseBody
    public ResponseEntity<?> register(@RequestBody Map<String, String> registerData) {
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);
        result.put("message", "注册成功，请登录");
        return ResponseEntity.ok(result);
    }
    
    private Map<String, Object> getUserStats() {
        Map<String, Object> stats = new HashMap<>();
        stats.put("totalTasks", 156);
        stats.put("completedTasks", 142);
        stats.put("totalArea", 500);
        stats.put("efficiency", 91);
        return stats;
    }
}